<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">资讯管理</a>
          <a><cite>评论管理</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body ">
        <button class="layui-btn layui-btn-danger layui-btn-sm delAll"><i class="layui-icon"></i>批量删除</button>
    </div>
    <div class="layui-card-body ">
        <table class="layui-hide" id="test" lay-filter="demo"></table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>
</body>
<script>
    layui.use(
        ['table', 'jquery', 'form', 'element'], function () {
            var table = layui.table
            $ = layui.jquery,
                form = layui.form;
            layui.element.render();
            table.render({
                elem: '#test'
                , url: '/admin/comment/tree'
                , cellMinWidth: 70
                , cols: [
                    [
                        {type: 'checkbox'}
                        , {field: 'id', title: 'ID', width: 90, unresize: true, sort: true}
                        , {field: 'username', title: '评论人'}
                        , {field: 'content', title: '内容'}
                        , {field: 'ip', title: 'IP'}
                        , {title: '操作', align: 'center', toolbar: '#barDemo'}
                    ]
                ]
                , page: true
            });
            //监听工具条
            table.on('tool(demo)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case 'detail':
                        layer.msg('ID：' + data.id + ' 的查看操作');
                        break;
                    case 'del':
                        layer.confirm('确定要删除该评论吗？', function (index) {
                            $.ajax({
                                url: '/admin/comment/delete',
                                method: 'post',
                                data: {id: data.id},
                                dataType: 'JSON',
                                success: function (res) {
                                    if (res.code == 0) {
                                        obj.del();
                                        layer.msg('删除成功', {icon: 1});
                                    } else {
                                        layer.msg(res.msg, {icon: 5});
                                    }
                                },
                                error: function (data) {
                                    layer.msg('服务器繁忙', {icon: 5});
                                }
                            });
                            layer.close(index);
                        });
                        break;
                    default:
                        break;
                }
            });
            $('.delAll').on('click', function () {
                var checkStatus = table.checkStatus('test')
                    , data = checkStatus.data
                    , ids = [];
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        ids.push(data[i].id);
                    }
                    layer.confirm('确定要删除' + data.length + '个评论吗？', function (index) {
                        $.ajax({
                            url: '/admin/comment/deleteAll',
                            method: 'post',
                            data: {ids: ids},
                            success: function (res) {
                                if (res.code == 0) {
                                    table.reload('test');
                                    layer.msg('删除成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 5});
                                }
                            },
                            error: function (data) {
                                layer.msg('服务器繁忙', {icon: 5});
                            }
                        });
                        layer.close(index);
                    });
                }
            });

        });
</script>
</html>